// packageA/pages/iotInstallAdd/iotInstallAdd.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //从设备列表来的传参
    paramObj:{},
    //传参来的id
    id:null,
    //是否是修改
    isEdit:false,
    //提交按钮禁用
    disabled:false,
    //避免表单重复提交
    code:null,
    //修改信息回显
    editInfo:null,
    //设备信息
    equipmentInfo:null,
    //安装类型选择框
    showType:false,
    //安装类型选择数据
    typeList:[],
    //安装位置选择框
    showPosition:false,
    //安装位置选择数据
    positionList:[],
    //登录人员
    user:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user=app.globalData.userInfo;
    this.setData({
      user:user,
      code:Base64.encode(new Date() + app.globalData.userInfo.userName)
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('安装信息-传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      if(paramObj.equipmentId){
        //获取设备数据
        this.getEquipmentInfo();
      }

      //获取要修改的数据
      if(paramObj.id){
        this.setData({
          id: paramObj.id,
          isEdit:true
        })
        this.getInfo();
      }else{
        console.log(1);
        //添加时-安装人员为当前登录用户
        let installUserObj='editInfo.installUserObj';
        this.setData({
          [installUserObj]: {
            id: user.id,
            userName: user.userName,
            personalPhone: user.personalPhone
          }
        })

        console.log(this.data.editInfo.installUserObj);
      }
    }
  },

  //提交表单
  confirmForm(e){
    //避免表单重复提交-先禁用按钮
    this.setData({
      disabled:true
    })

    let td=this.data;
    let editInfo=td.editInfo;
    let eInfo=td.equipmentInfo;
    let user=app.globalData.userInfo;  

    if(editInfo && editInfo.installTypeObj){      
    }else{
      Toast.fail('请选择安装类型');
      return;
    }

    if(editInfo && editInfo.installPositionObj){      
    }else{
      Toast.fail('请选择安装位置');
      return;
    }

    let dataObj={
      requestToken: td.code,
      id: this.data.id,      
      imei: eInfo.imei,
      equipmentNo: eInfo.equipmentNo,
      member: eInfo.member,
      project: eInfo.project,
      installType: editInfo.installTypeObj.id,
      installTypeObj: editInfo.installTypeObj,
      installPosition: editInfo.installPositionObj.id,
      installPositionObj: editInfo.installPositionObj,
      installPositionPhoto: editInfo.installPositionPhoto,
      installGroupPhoto: editInfo.installGroupPhoto,
      chatRecordPhoto: editInfo.chatRecordPhoto,
      dataAuthority: eInfo.dataAuthority
    }

    if(!td.isEdit){
      dataObj.installUserId=user.id;
      dataObj.installUserObj=editInfo.installUserObj;
      dataObj.equipmentId=eInfo.id;
    }

    let param={
      className:'iotInstall',
      data: dataObj
    }

    // console.log(param.data);
    // return;

    //避免表单重复提交
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '数据提交中...'
    });

    CommonService.goSave(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('成功');
        setTimeout(() => {
          //返回列表页并刷新数据
          this.backAndRefresh();
        }, 1000);
      }else if(res.statusCode==202){
        Toast.fail('您操作太快了,请勿重复提交');
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })
  },

  //获取设备数据
  getEquipmentInfo(){
    let param = {
      isAuth: false,
      className: 'equipment',
      data: {
        id:this.data.paramObj.equipmentId,
        selectFields: ['id','imei','equipmentNo','member','project','dataAuthority']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('设备信息');
        console.log(result[0]);

        this.setData({
          equipmentInfo: result[0]
        })
      }
    }).catch((error) => {
    })
  },

  //获取要修改的数据
  getInfo(){
    let param = {
      isAuth: false,
      className: 'iotInstall',
      data: {
        id:this.data.paramObj.id,
        selectFields: ['id','equipmentId','imei','equipmentNo','member','project','installUserObj','installTypeObj','installPositionObj','installPositionPhoto','installGroupPhoto','chatRecordPhoto']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('安装信息回显');
        console.log(result[0]);

        this.setData({
          editInfo: result[0]
        })
      }
    }).catch((error) => {
    })
  },

  //显示安装位置选择框
  onShowPosition(){
    this.setData({
			showPosition: true
    })
    this.getPositionList();
  },

  //查询安装位置
  getPositionList(){
    let param = {
      isAuth: false,
      className: 'dictionary',
      data: {
        searchAll: true,
        key: 'installPart',
        selectFields: ['id', 'name']
      }
    }
    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let arr=[];
        result.forEach(item => {
          arr.push({
            text:item.name,
            id:item.id
          });
        });
        this.setData({
          positionList: arr
        })
      }
    }).catch((error) => {
    })
  },

  //隐藏安装位置选择框
  onClosePosition(){
    this.setData({
			showPosition: false
    })
  },

  //确认安装位置
  onConfirmPosition(e){
    let installPositionObj='editInfo.installPositionObj';
    this.setData({
      [installPositionObj]:e.detail.value,
      disabled:false
    })
    this.onClosePosition();
  },

  //获取上传图片-安装线路关键位置
  getInstallPositionImg(data){
    let installPositionPhoto='editInfo.installPositionPhoto';
    if(data.detail.length){
      this.setData({
        [installPositionPhoto]: data.detail
      })
    }else{
      this.setData({
        [installPositionPhoto]: null
      })
    }
  },

  //获取上传图片-安装人员与设备合影
  getInstallGroupPhoto(data){ 
    let installGroupPhoto='editInfo.installGroupPhoto';
    if(data.detail.length){
      this.setData({
        [installGroupPhoto]: data.detail
      })
    }else{
      this.setData({
        [installGroupPhoto]: null
      })
    }
  },

  //获取上传图片-聊天记录图片
  getChatRecordPhoto(data){
    let chatRecordPhoto='editInfo.chatRecordPhoto';
    if(data.detail.length){
      this.setData({
        [chatRecordPhoto]: data.detail
      })
    }else{
      this.setData({
        [chatRecordPhoto]: null
      })
    }
  },

  //返回列表页并刷新数据
  backAndRefresh(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];

    prevPage.getList();
    wx.navigateBack();
  },

  //显示安装类型选择框
  onShowType(){
    this.setData({
			showType: true
    })
    this.getTypeList();
  },
  
  //查询安装位置
  getTypeList(){
    let param = {
      isAuth: false,
      className: 'dictionary',
      data: {
        searchAll: true,
        key: 'installType',
        selectFields: ['id', 'name']
      }
    }
    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let arr=[];
        result.forEach(item => {
          arr.push({
            text:item.name,
            id:item.id
          });
        });
        this.setData({
          typeList: arr
        })
      }
    }).catch((error) => {
    })
  },

  //隐藏安装类型选择框
  onCloseType(){
    this.setData({
			showType: false
    })
  },

  //确认安装类型
  onConfirmType(e){
    let installTypeObj='editInfo.installTypeObj';
    this.setData({
      [installTypeObj]:e.detail.value,
      disabled:false
    })
    this.onCloseType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
